package com.iteaj.iot.test.modbus;

import com.iteaj.iot.ProtocolType;
import com.iteaj.iot.client.modbus.ModbusCode;
import com.iteaj.iot.client.modbus.ModbusTcpMessageBuilder;
import com.iteaj.iot.client.modbus.consts.ModbusCoilStatus;
import com.iteaj.iot.client.protocol.ClientInitiativeProtocol;
import com.iteaj.iot.test.TestProtocolType;

public class ModbusStatusProtocol extends ClientInitiativeProtocol<ModbusTestMessage> {

    private ModbusCode code;

    public ModbusStatusProtocol(ModbusCode code) {
        this.code = code;
    }

    @Override
    protected ModbusTestMessage doBuildRequestMessage() {
        switch (code) {
            case Read01:
                return ModbusTcpMessageBuilder.buildRead01Message(ModbusTestMessage.class,1, 0, 24);
            case Read02:
                return ModbusTcpMessageBuilder.buildRead02Message(ModbusTestMessage.class,1, 0, 18);
            case Read03:
                return ModbusTcpMessageBuilder.buildRead03Message(ModbusTestMessage.class,1, 0, 1);
            case Read04:
                return ModbusTcpMessageBuilder.buildRead04Message(ModbusTestMessage.class,1, 0, 1);
            case Write05:
                return ModbusTcpMessageBuilder.buildWrite05Message(ModbusTestMessage.class, 1, 0, ModbusCoilStatus.OFF);
            case Write06:
                return ModbusTcpMessageBuilder.buildWrite06Message(ModbusTestMessage.class,1, 1, new byte[]{(byte) 0x00, (byte) 0x06});
            case Write0F:
                return ModbusTcpMessageBuilder.buildWrite0FMessage(ModbusTestMessage.class,1, 8, 5, new byte[]{(byte) 0x0F});
            case Write10:
                return ModbusTcpMessageBuilder.buildWrite10Message(ModbusTestMessage.class, 1, 2, 1, new byte[]{(byte) 0x00, (byte) 0x10});
            default: throw new IllegalStateException("不支持功能码["+code+"]");
        }
    }

    @Override
    public void doBuildResponseMessage(ModbusTestMessage responseMessage) {

    }

    @Override
    public ProtocolType protocolType() {
        return TestProtocolType.CIReq;
    }
}
